package com.weather.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.weather.domain.User;
import com.weather.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @RequestMapping("doLogin")
    public SaResult doLogin(String username, String password) {
        // 假设有一个UserService实例来处理用户的业务逻辑
        User user = userService.selectByUsername(username,password);
        System.out.println("userdata"+ user);

        if (user != null && password.equals(user.getPassword())) {
            // 用户存在且密码正确，创建登录会话
            StpUtil.login(username); // 假设用户对象有一个getId()方法获取用户ID
            return SaResult.ok().set("token", StpUtil.getTokenInfo());
        }
        return SaResult.error("登录失败");
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}
